package org.hackystat.sdsa.analyzer.model.activity.edit;

import java.util.Map;
import java.util.logging.Logger;

import org.hackystat.sdsa.analyzer.control.SensorDataUtil;
import org.hackystat.sdsa.analyzer.model.activity.Activity;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;

/**
 * Defines editing activities.
 * 
 * @author hongbing
 */
public abstract class EditActivity extends Activity {
  /** Duration in seconds. */
  private int duration;
  /** File size with this edit. */
  protected int fileSize = 0;
  /** File size increase. It could be either positive or negative. */
  protected int fileSizeIncrease = 0;
  /** Current-Size property.*/
  public static final String CURRENT_SIZE = "current-size";
  /** Edit Activity Type */
  private static final String EDIT_TYPE = "Edit";

  /**
   * Empty constructor.
   */
  public EditActivity() {
    super();
    super.setType(EDIT_TYPE);
  }
  
  /**
   * Instantiates the edit activity from sensor data.
   * 
   * @param sensorData Sensor data.
   * @param logger Logger.
   */
  public EditActivity(SensorData sensorData, Logger logger) {
    super(sensorData, logger);
    super.setType(EDIT_TYPE);
    
    Map<String, String> propMap = SensorDataUtil.propMap(sensorData);
    String size = propMap.get(CURRENT_SIZE);
    this.fileSize = Integer.parseInt(size);
  }

  /**
   * Set the size of the file being edited. 
   * 
   * @param fileSize Size of the file being edited.
   */
  public void setFileSize(int fileSize) {
    this.fileSize = fileSize;
  }
  
  /**
   * Gets edited file size.
   *  
   * @return Size of the file is edited.
   */
  public int getFileSize() {
    return this.fileSize;
  }
  
  /**
   * Sets file size increase with this edit action. 
   * 
   * @param increase Increase of file size.
   */
  public void setFileSizeIncrease(int increase) {
    this.fileSizeIncrease = increase; 
  }
  
  /**
   * Gets file size increase with this edit action.
   * 
   * @return File size increase.
   */
  public int getFileSizeIncrease() {
    return this.fileSizeIncrease;
  }
  
  /**
   * Sets duration of this action. 
   * 
   * @param duration Duration of this action in number of seconds.
   */
  public void setDuration(int duration) {
    this.duration = duration;
  }

  /**
   * Gets duration of action in seconds.
   * 
   * @return Duration in seconds.
   */
  public int getDuration() {
    return this.duration;
  }
  
  /**
   * Gets edit action string.
   * 
   * @return Edit action string. 
   */
  @Override public String toString() {
    StringBuffer buf = new StringBuffer();
    buf.append(super.toString());
    buf.append(' ').append(this.duration).append('s');

    return buf.toString();
  }
  
  /**
   * Gets metric name=value string pair.
   * @param name Metric name
   * @param value value of the metric.
   * @param total Total value of the metric.
   * @return Metric name=value(total) pair.
   */
  protected String makeMetricPair(String name, int value, int total) {
    StringBuffer buf = new StringBuffer();
    buf.append(name);
    buf.append('=');
    if (value > 0) {
      buf.append('+');
    }
    buf.append(value);
    
    buf.append('(').append(total).append(')');    
    return buf.toString();
  }
  
  /**
   * Checks whether this edit work makes any progress.
   * 
   * @return True if there is any or false otherwise.
   */
  public abstract boolean hasProgress();

  /**
   * Checks whether this edit work is significant.
   * 
   * @return True if it is and false otherwise.
   */
  public abstract boolean isSubstantial();
}
